#pragma once

#include "Xml.h"
#include "Http.h"

enum PostVis {
    Publich, Draft
};

class IPost
{
public:
    virtual ~IPost() {}
    virtual void Post(const std::wstring &title, PostVis v) = 0;
};

class GDataPost : public IPost
{
public:
    virtual ~GDataPost() {}
    void Post(const std::wstring &title, PostVis v)
    {
        const std::wstring name = L"";
        const std::wstring email = L"";
        const std::wstring passwd = L"";
        const std::wstring postUrl = L"";
        IX *xhtml = xt(L"Hello World!");

        wstring authReq = 
            L"accountType=" + UrlEncode(L"HOSTED_OR_GOOGLE") + 
            L"&Email=" + UrlEncode(email) +
            L"&Passwd=" + UrlEncode(passwd) +
            L"&source=" + UrlEncode(L"quickwrite-1") +
            L"&service=" + UrlEncode(L"blogger");

        HttpReq a(L"https://www.google.com/accounts/ClientLogin");
        a.Post(authReq, L"Content-Type: application/x-www-form-urlencoded\r\n");

        const wchar_t AUTH[] = L"Auth=";
        vector<char>::iterator ai = search(a.respData_.begin(), a.respData_.end(), AUTH, AUTH+5);
        if (ai != a.respData_.end()) {
            wstring authHdr = L"Authorization: GoogleLogin auth=" + std::wstring(ai+5, a.respData_.end()) + L"\r\n";
            wstring ctyHdr = L"Content-Type: application/atom+xml\r\n";
            wstring hdr = authHdr + ctyHdr;
            
            IX *dx = (v == Draft) ?
                xl(L"<app:control xmlns:app='http://purl.org/atom/app#'><app:draft>yes</app:draft></app:control>") :
                0;

            XE entry(L"entry", xa(L"xmlns", L"http://www.w3.org/2005/Atom"),
                xe(L"title", xa(L"type", L"text"), xt(title)),
                dx,
                xe(L"content", xa(L"type", L"xhtml"), xhtml),
                xe(L"author", 
                    xe(L"name", xt(name)),
                    xe(L"email", xt(email))));

            HttpReq e(postUrl);
            e.Post(OuterXml(entry), hdr);
            e.respStatusCode_=5;
        }
    }
};
